var url;
var id;

$(function() {
	
	//初始化====================================
	$(".datepicker").datepicker();
	$("#type").buttonset();
	$("#dataGird").flexigrid({
		url: "/project/project/selectForPage.json",
		colModel : [
		    {display: "ID", name : "id", width : 90, sortable : true,hide: true},
	        {display: "项目名称", name : "name", width : 400, sortable : true},
	        {display: "控制类型", name : "type", width : 90, sortable : true,process:formatType},
			{display: "总产值", name : "totalValue", width : 90, sortable : true,process:formatCost},
			{display: "已完成(%)", name : "completedValue", width : 90, sortable : true, hide: false,process:formatPercent},
			{display: "预核算完成", name : "budgetcalcCplValue", width : 90, sortable : true,process:formatCost},
			//{display: "合计", name : "total", width : 120, sortable : true},
			{display: "创建时间", name : "gmtCreate", width : 120, sortable : true},
			{display: "创建人", name : "creator", width : 120, sortable : true},
			{display: "修改时间", name : "gmtCreate", width : 120, sortable : true},
			{display: "修改人", name : "modifier", width : 120, sortable : true}
			]
    }); 
	
	$('#projectDialog').dialog({
		autoOpen: false,
		width: 600,
		height: 350,
		modal: true
	});
		
	$( "#dialogConfirm" ).dialog({
		resizable: false,
		autoOpen: false,
		height:180,
		modal: true,
		buttons: {
			"确认删除": function() {
				MZ.ajaxShowMsg({
					url : "/project/project/delete",
					data:{id:$("#dataGird").flexigrid().getRowValue(2)},
					success:function(data){
						MZ.closeDialog("dialogConfirm");
						MZ.searchGird("searchForm","dataGird");
					},
					failure:function(data){
						MZ.closeDialog("dialogConfirm");
					}
				});
			},
			"取消": function() {
				MZ.closeDialog("dialogConfirm");
			}
		}
	});
	
	
	//绑定金额输入框keyup事件
	//当输入框数量more时，需要采用total-old+new更新total值
	//不宜再全局运算
	$("#projectForm input.currency-type").keyup(function(event){ 
		//console.log("oldVal in key up: %d", oldVal);
		var reg = /^-?\d+\.{0,}\d{0,}$/;
		if(!reg.test($(this).val())){
			$(this).val("");
		}
	});
	$("#totalValue").keyup(function(event){ 
		var cpVal = $("#completedValue").val();
		if(cpVal == ""){
			return false;
		}else{
			cpVal = cpVal * 1;
			$("#budgetcalcCplValue").val(cpVal*($(this).val()*1)/100);
		}
	});
	$("#completedValue").keyup(function(event){ 
		if ($(this).val()*1 > 100){
			MZ.showMsg("已完成值百分比范围(0-100)%!");
			$(this).val(0)
			return false;
		}
		var totalVal = $("#totalValue").val();
		if(totalVal == ""){
			MZ.showMsg("总产值不能为空!");
			return false;
		}
		$("#budgetcalcCplValue").val(totalVal*($(this).val()*1)/100);
	});
	
	
	
	//事件监控====================================
	//重置表单
	$("#clearButton").bind("click",function(){
		MZ.reset("searchForm");
	});
	
	//查询数据
	$("#searchButton").bind("click",function(){
		MZ.searchGird("searchForm","dataGird");
	});
	
	//打开新增数据对话框
	$("#add").bind("click", function(){
		$("#ui-dialog-title-projectDialog").text("新增项目");
		MZ.clearForm("projectForm");
		$("#resetBtn").show();
		$('#type1').click();
		isupdate=false;
		MZ.openDialog("projectDialog");
	});
	
	//打开修改数据对话框
	$("#update").bind("click", function(){
		if ($("#dataGird").flexigrid().getRowValue(2).length<=0){
			MZ.showMsg("请选择要修改的数据");
			return ;
		}
		if ($("#dataGird").flexigrid().getRowValue(2).length>1){
			MZ.showMsg("无法修改多条数据，请选择一条要修改的数据");
			return ;
		}
		params={id:$("#dataGird").flexigrid().getRowValue(2)[0]};
		id=params.id;
		isupdate=true;
		//填充数据
		$.ajax({
			url : "/project/project/selectById",
			type : "post",
			data : params,
			success : function(result) {
				if(result.succeed){
					for(var key in result.data){
						//if (key=="totalValue" || key=="completedValue" || key=="budgetcalcCplValue"){
						if (key=="totalValue"){
							$("#"+key).val(MZ.toDecimal(result.data[key]/MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND));
						}else if(key=="budgetcalcCplValue"){
							$("#"+key).val(MZ.toDecimal(result.data[key]/MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND));
						}else{
							$("#"+key).val(result.data[key]);
						}
						if (key=="type"){
							if (result.data[key]==1){
								$('#type1').click();
							}else if (result.data[key]==2){
								$('#type2').click();
							}
						}
					}
					$("#ui-dialog-title-projectDialog").text("修改项目");
					$("#resetBtn").hide();
					MZ.openDialog("projectDialog");
				}else{
					MZ.showMsg("无法加载数据");
				}
			},
			error: function(result){
				MZ.showMsg("无法加载数据");
			}
		});
		//-----------------
	});
	
	//删除数据
	$("#delete").bind("click", function(){
		if ($("#dataGird").flexigrid().getRowValue(2).length<=0){
			MZ.showMsg("请选择要删除的数据");
			return ;
		}
		$("#dialogConfirm").text("该操作将删除选定的用户信息，确定这样做?");
		MZ.openDialog("dialogConfirm");
	});
	
	//提交数据
	$("#submitBtn").bind("click", function(){
		var params=$('#projectForm').serializeObject();
		var type=$('#type').find(':checked').val();
		params['type']=type;
		if (isupdate){
			url="/project/project/updateById";
			params['id']=id;
		}else{
			url="/project/project/insert";
		}
		if (!validata(params)){
			return;
		}
		params["totalValue"] = params["totalValue"] * MZ.CURRENCY_PRECISION;
		params["completedValue"] = params["completedValue"] ;//* MZ.CURRENCY_PRECISION;
		params["budgetcalcCplValue"] = params["budgetcalcCplValue"] * MZ.CURRENCY_PRECISION ;//* MZ.CURRENCY_PRECISION;
		MZ.ajaxShowMsg({
			url : url,
			data:params,
			success:function(){
				MZ.closeDialog("projectDialog");
				MZ.searchGird("searchForm","dataGird");	
			}
		});
	});
	
	//取消
	$("#cancelBtn").bind("click", function(){
		MZ.closeDialog("projectDialog");
	});
	//重置
	$("#resetBtn").bind("click", function(){
		MZ.reset("projectForm");
		$('#type1').click();
	});
	
	function validata(data){
		if (MZ.isBlank(data.name)){
			alert("项目名称不能为空");
			return false;
		}
		if (MZ.isBlank(data.type)){
			alert("请选择控制类型");
			return false;
		}
		//
		if (MZ.isBlank(data.totalValue)){
			alert("请输入总产值");
		}
		if (!MZ.isNumber(data.totalValue)){
			alert("总产值必须是数字");
			return false;
		}
		//
		if (MZ.isBlank(data.completedValue)){
			alert("请输入已完成值");
		}
		if (!MZ.isNumber(data.completedValue)){
			alert("已完成值必须是数字");
			return false;
		}else{
			if(data.completedValue>100){
				MZ.showMsg("已完成值百分比范围(0-100%)");
				return false;
			}
		}
		//
		/*
		if (MZ.isBlank(data.budgetcalcCplValue)){
			alert("请输入预核算完值");
		}
		if (!MZ.isNumber(data.budgetcalcCplValue)){
			alert("预核算完成值必须是数字");
			return false;
		}else{
			if(data.budgetcalcCplValue>100){
				MZ.showMsg("预核算完成值百分比范围(0-100%)");
				return false;
			}
		}
		 */
		
		return true;
	}
	
});

function onSelectRow(tr){
	//
}

function formatCost(tdDiv, pid){
	var cost = $(tdDiv).text();
	if(cost == "0"){
		return "";
	}
	return MZ.toDecimal(cost/MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND);
}
function formatPercent(tdDiv, pid){
	var val = $(tdDiv).text();
	if(val != ""){
		val += "%";
	}
	return val;
}

function formatType(tdDiv, pid){
	var type = $(tdDiv).text();
	if (type==1){
		return "预算";
	}else if(type=2){
		return "核算";
	}else{
		return "未知";
	}
}